package info.thereisonlywe.onenesspathfinder.ui;

import info.thereisonlywe.core.project.ProjectConstants;
import info.thereisonlywe.core.project.ProjectLocalization;
import info.thereisonlywe.core.toolkit.GUIToolkit;
import info.thereisonlywe.quran.bookmark.QuranicBookmark;
import info.thereisonlywe.quran.bookmark.QuranicBookmarkManager;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;

public class SaveBookmarksDialog {

	private static JDialog dialog;
	private static JButton okButton;
	private static JPanel panel;
	private static JRadioButton rdbtnNewRadioButton;
	private static JComboBox<String> comboBox;
	private static JRadioButton radioButton;
	private static final JTextField textField = new JTextField();
	private static QuranicBookmark[] bookmarksToBeWritten;

	/**
	 * @wbp.parser.entryPoint
	 */
	private static void init()
	{
		textField.setColumns(10);
		if (dialog != null) return;
		dialog = new JDialog();
		dialog.setResizable(false);
		dialog.setIconImage(null);
		dialog.setTitle(ProjectLocalization.getString(
				ProjectConstants.ONENESS_PATHFINDER_PROJECT, "SaveBookmarks"));
		dialog.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		dialog.setPreferredSize(new Dimension(350, 200));
		dialog.setSize(new Dimension(425, 200));
		dialog.setModal(true);
		dialog.getContentPane().setLayout(new BorderLayout());
		JPanel buttonPane = new JPanel();
		buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT, 10, 0));
		dialog.getContentPane().add(buttonPane, BorderLayout.SOUTH);

		okButton = new JButton(ProjectLocalization.getString(
				ProjectConstants.CORE_PROJECT, "OK"));
		okButton.setActionCommand("OK");
		buttonPane.add(okButton);

		panel = new JPanel();
		dialog.getContentPane().add(panel, BorderLayout.CENTER);

		rdbtnNewRadioButton = new JRadioButton(ProjectLocalization.getString(
				ProjectConstants.ONENESS_PATHFINDER_PROJECT, "Save.Append"));

		comboBox = new JComboBox<>();

		radioButton = new JRadioButton(ProjectLocalization.getString(
				ProjectConstants.ONENESS_PATHFINDER_PROJECT, "Save.New"));
		radioButton.setSelected(true);
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(gl_panel
				.createParallelGroup(Alignment.LEADING)
				.addGroup(
						gl_panel.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										gl_panel.createParallelGroup(
												Alignment.LEADING)
												.addGroup(
														Alignment.TRAILING,
														gl_panel.createSequentialGroup()
																.addComponent(
																		rdbtnNewRadioButton,
																		GroupLayout.DEFAULT_SIZE,
																		332,
																		Short.MAX_VALUE)
																.addContainerGap())
												.addGroup(
														Alignment.TRAILING,
														gl_panel.createSequentialGroup()
																.addGap(22)
																.addComponent(
																		comboBox,
																		0,
																		0,
																		Short.MAX_VALUE)
																.addGap(283))
												.addGroup(
														gl_panel.createSequentialGroup()
																.addGroup(
																		gl_panel.createParallelGroup(
																				Alignment.LEADING)
																				.addGroup(
																						gl_panel.createSequentialGroup()
																								.addGap(22)
																								.addComponent(
																										textField,
																										GroupLayout.PREFERRED_SIZE,
																										0,
																										Short.MAX_VALUE)
																								.addGap(188))
																				.addComponent(
																						radioButton,
																						Alignment.TRAILING,
																						GroupLayout.DEFAULT_SIZE,
																						332,
																						Short.MAX_VALUE))
																.addContainerGap()))));
		gl_panel.setVerticalGroup(gl_panel.createParallelGroup(
				Alignment.LEADING).addGroup(
				gl_panel.createSequentialGroup()
						.addContainerGap()
						.addComponent(rdbtnNewRadioButton,
								GroupLayout.PREFERRED_SIZE, 26,
								GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(comboBox, GroupLayout.PREFERRED_SIZE,
								GroupLayout.DEFAULT_SIZE,
								GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.UNRELATED)
						.addComponent(radioButton, GroupLayout.PREFERRED_SIZE,
								26, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(textField, GroupLayout.PREFERRED_SIZE,
								GroupLayout.DEFAULT_SIZE,
								GroupLayout.PREFERRED_SIZE)
						.addContainerGap(13, Short.MAX_VALUE)));
		panel.setLayout(gl_panel);
		ButtonGroup bg = new ButtonGroup();
		bg.add(radioButton);
		bg.add(rdbtnNewRadioButton);
		initListeners();
	}

	private static void initListeners()
	{

		KeyboardFocusManager.getCurrentKeyboardFocusManager()
				.addKeyEventDispatcher(new KeyEventDispatcher()
				{
					@Override
					public boolean dispatchKeyEvent(KeyEvent e)
					{
						boolean keyHandled = false;
						if (e.getID() == KeyEvent.KEY_PRESSED
								&& dialog.isVisible())
						{
							if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
							{
								dialog.setVisible(false);
								UIDelegate.setOpenDialog(false);
								keyHandled = true;
							}
						}
						return keyHandled;
					}
				});

		okButton.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent e)
			{
				if (rdbtnNewRadioButton.isEnabled()
						&& rdbtnNewRadioButton.isSelected())
				{
					QuranicBookmarkManager.appendToFile(QuranicBookmarkManager
							.getBookmarkListFile((String) comboBox
									.getSelectedItem()), bookmarksToBeWritten);
				}
				else
				{
					if (QuranicBookmarkManager.getBookmarkListFile(
							textField.getText()).exists())
					{
						UIDelegate
								.showConfirmDialog("Save.OverwriteConfirmation");
					}
					else
					{
						QuranicBookmarkManager.createNewFileAndWrite(
								QuranicBookmarkManager
										.getBookmarkListFile(textField
												.getText()),
								bookmarksToBeWritten);
					}
				}
				MainFrame.view_Windows.getSearchFilterPanel().refreshValues();
				dialog.setVisible(false);
				UIDelegate.setOpenDialog(false);
			}
		});

		dialog.addWindowListener(new WindowAdapter()
		{

			@Override
			public void windowClosing(WindowEvent e)
			{
				dialog.setVisible(false);
				UIDelegate.setOpenDialog(false);
			}
		});

	}

	public static void showDialog(QuranicBookmark[] bookmarksToBeWritten)
	{
		init();
		SaveBookmarksDialog.bookmarksToBeWritten = bookmarksToBeWritten;
		comboBox.setModel(new DefaultComboBoxModel<String>(
				QuranicBookmarkManager.getExistingBookmarkListNames()));
		if (comboBox.getItemCount() <= 0) rdbtnNewRadioButton.setEnabled(false);
		else rdbtnNewRadioButton.setEnabled(true);
		dialog.setLocation(GUIToolkit.getCenteredWindowPosition(
				dialog.getWidth(), dialog.getHeight()));
		UIDelegate.setOpenDialog(true);
		dialog.setVisible(true);
	}
}
